package cn.iocoder.yudao.module.product.controller.app.product;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.product.controller.app.product.vo.SecondProductPageReqVO;
import cn.iocoder.yudao.module.product.controller.app.product.vo.SecondProductReqVO;
import cn.iocoder.yudao.module.product.controller.app.product.vo.SecondProductViewHistoryReqVO;
import cn.iocoder.yudao.module.product.controller.vo.SecondProductPageRespVO;
import cn.iocoder.yudao.module.product.dal.dataobject.product.SecondProductDO;
import cn.iocoder.yudao.module.product.dal.dataobject.product.SecondProductViewHistoryDO;
import cn.iocoder.yudao.module.product.service.product.SecondProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "用户 App - 闲置交易")
@RestController
@RequestMapping("/second")
@Validated
public class SecondProductController {

    @Resource
    private SecondProductService secondProductService;

    @PostMapping("/push")
    @Operation(summary = "发布闲置商品")
    public CommonResult<Boolean> push(@Valid @RequestBody SecondProductReqVO secondProductReqVO) {
        secondProductReqVO.setCreator(String.valueOf(getLoginUserId()));
        secondProductService.push(secondProductReqVO);
        return success(true);
    }

    @PostMapping("/delete")
    @Operation(summary = "删除闲置商品")
    @Parameter(name = "id", description = "商品编号", required = true)
    public CommonResult<Boolean> push(@RequestParam("id") Long id) {
        SecondProductReqVO secondProductReqVO = new SecondProductReqVO();
        secondProductReqVO.setUpdater(String.valueOf(getLoginUserId()));
        secondProductService.delete(id,secondProductReqVO);
        return success(true);
    }

    @PostMapping("/changeStatus")
    @Operation(summary = "修改闲置商品状态")
    @Parameters({
            @Parameter(name = "id", description = "商品编号", required = true),
            @Parameter(name = "status", description = "商品状态", required = true),
    })
    public CommonResult<Boolean> push(@RequestParam("id") Long id,@RequestParam("status") Integer status) {
        secondProductService.changeStatus(id,status);
        return success(true);
    }

    @PostMapping("/addViewHistory")
    @Operation(summary = "添加查看记录")
    @Parameters({
            @Parameter(name = "id", description = "商品编号", required = true),
    })
    public CommonResult<Boolean> addViewHistory(@RequestParam("id") Long id) {
        secondProductService.addViewHistory(id);
        return success(true);
    }

    @GetMapping("/list")
    @Operation(summary = "商品查看")
    @Parameters({
            @Parameter(name = "id", description = "商品编号", required = true),
            @Parameter(name = "status", description = "商品状态", required = true),
    })
    public CommonResult<PageResult<SecondProductPageRespVO>> getProductPage(SecondProductPageReqVO pageVO) {
        PageResult<SecondProductPageRespVO> list = secondProductService.getProductPage(pageVO);
        return success(list);
    }

    @GetMapping("/detailById")
    @Operation(summary = "商品详情")
    @Parameters({
            @Parameter(name = "id", description = "商品编号", required = true)
    })
    public CommonResult<SecondProductPageRespVO> detailById(SecondProductPageReqVO pageVO) {
        SecondProductPageRespVO data = secondProductService.detailById(pageVO);
        return success(data);
    }


}
